package com.matt.controller.api.web;

import com.matt.commons.dto.sso.StaffDTO;
import com.matt.foundation.model.StatusResponse;
import com.matt.remoteCall.SsoCallService;
import com.matt.repository.CountProductRepository;
import com.matt.service.ContractProductService;

import java.util.HashMap;
import java.util.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.matt.foundation.constant.CommonConstant.TICKET;

/**
 * ***************************
 * description :合同产品控制器
 * Created by Ron
 * from com.matt
 * on 2018/6/20 0020.
 * *************************
 */
@RestController
@RequestMapping("/web/contract_product")
@Api(tags = "CRM - 合同产品")
public class ContractProductController {

    @Autowired
    private ContractProductService contractProductService;

    @Autowired
    private CountProductRepository countProductRepository;

    @Autowired
    private SsoCallService ssoCallService;

    /**
     * 删除合同产品
     *
     * @param contractProductId 合同产品id
     * @return
     */
    @RequestMapping(value = "/del_contract_product_byId")
    @ApiOperation(value = "删除合同产品", httpMethod = "DELETE")
    public StatusResponse deleteById(@RequestHeader(TICKET) String ticket, String contractProductId) {
        Boolean result = contractProductService.deleteById(contractProductId);
        return StatusResponse.success(result);
    }


//    @RequestMapping(value = "/getAll")
//    @ApiOperation(value = "删除合同产品", httpMethod = "GET")
//    public StatusResponse getAll() {
//        return StatusResponse.success(contractProductService.findAll());
//    }

    /**
     * 查询产品
     *
     * @param ticket
     * @param clienteleId 客户id
     * @return
     */
    @RequestMapping(value = "/find_by_clienteleId")
    @ApiOperation(value = "查询客户购买的产品", httpMethod = "GET")
    public StatusResponse findByClienteleId(@RequestHeader(TICKET) String ticket, String clienteleId) {
        return StatusResponse.success(contractProductService.findByClienteleId(clienteleId));
    }

    /**
     * 查询产品
     *
     * @param contractId 合同id
     * @return
     */
    @RequestMapping(value = "/find_By_contractId")
    @ApiOperation(value = "查询合同内的产品", httpMethod = "GET")
    public StatusResponse findByContract(@RequestHeader(TICKET) String ticket, String contractId) {
        return StatusResponse.success(contractProductService.findByContractId(contractId));
    }


    /**
     * 查询购买数量最多产品
     *
     * @return
     */
    @RequestMapping(value = "/count_By_sale")
    @ApiOperation(value = "查询购买数量最多产品", httpMethod = "GET")
    public StatusResponse countBySale(@RequestHeader(TICKET) String ticket) {

        StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        String areaId = staff.getAreaId();

        Map<String,Object> m = new HashMap();
    	m.put("list", countProductRepository.countMostProduct(areaId));
    	m.put("total", countProductRepository.countTotal(areaId).get(0).getCountNum());
        return StatusResponse.success(m);
    }
    
    /**
     * 查询销课数量最多产品
     * @return
     */
    @RequestMapping(value = "/count_By_pin")
    @ApiOperation(value = "查询销课数量最多产品", httpMethod = "GET")
    public StatusResponse countByPin(@RequestHeader(TICKET) String ticket) {

        StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        String areaId = staff.getAreaId();

        Map<String,Object> m = new HashMap();
    	m.put("list", countProductRepository.countSaleProduct(areaId));
    	m.put("total", countProductRepository.countSaleTotal(areaId).get(0).getCountNum());

        return StatusResponse.success(m);
    }
}
